<?php

namespace backend\models;

use common\server\BasicDataServer;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\BannerModel;

/**
 * SearchHouseModel represents the model behind the search form about `common\models\HouseModel`.
 */
class SearchBannerModel extends BannerModel
{
    public  $mode = 0;
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'region', 'houseid'], 'integer'],
            [['banner'], 'string', 'max' => 100],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = BannerModel::find();
//      $query->leftJoin('ph_base_region', 'ph_region_banner.region = ph_base_region.id');
		$query->joinWith(['baseRegion']);
		$query->joinWith(['house']);
		$query->select("ph_base_region.*,ph_house.house_name,ph_region_banner.*");
        $query->orderBy(['ph_region_banner.id'=>SORT_DESC]);
        
        $this->load($params);
        
        if(!empty($this->region)){
            $query->andWhere(['region'=>$this->region]);
        }
        
//      $sqls = "select * from ph_region_banner LEFT JOIN ph_base_region on ph_region_banner.region = ph_base_region.id";
        // add conditions that should always apply here
//		$query = BannerModel::findBySql($sqls);
//      $query = BannerModel::findBySql($sqls);
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
		
		
		
		
        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }


        //$query->andFilterWhere(['like', 'house_name', $this->house_name]);
//      if(!empty($this->region)){
//          $query->andWhere(['region'=>BasicDataServer::instance()->getChildRegion($this->region_id)]);
//      }
       
        return $dataProvider;
    }
	
}
